#include <ratio>
#include <optional>
#include "VanEmdeBoasTree.h"

bool VanEmdeBoasTree<0>::member(size_t x) const
{
	if (!_pMin || !_pMax)
		return false;

	if (x == *_pMin || x == *_pMax)
		return true;

	return false;
}

auto VanEmdeBoasTree<0>::successor(size_t x) const->std::optional<size_t>
{
	if (!x && _pMax && *_pMax == 1)
		return 1;

	return std::nullopt;
}

auto VanEmdeBoasTree<0>::predecessor(size_t x) const->std::optional<size_t>
{
	if (x && _pMin && *_pMin == 0)
		return 0;

	return std::nullopt;
}

bool VanEmdeBoasTree<0>::insert(size_t x)
{
	if (!_pMin)
		return insertEmpty(x);

	if (*_pMax < x)
		*_pMax = x;
	return true;
}

void VanEmdeBoasTree<0>::erase(size_t x)
{
	if (!_pMin || !_pMax)
		return;

	if (*_pMin == *_pMax)
	{
		_pMin.reset();
		_pMax.reset();
		return;
	}

	*_pMin = x ? 0 : 1;
	*_pMax = *_pMin;
}


